/*
参数说明：
alertShow-弹出框参数{
  showmsg-服务器弹窗提示
  msg-服务器提示内容
}
*/
<template>
  <div>
    <alert v-model="alertShow.showmsg" :title="'提示'" @on-show="onShow" @on-hide="onHide">{{alertShow.msg}}</alert>
    <div  style="font-size: 0">
      <img  v-for="subjectitem in subjectList" :src="subjectitem.picUrl" @click="shopinfoFn(subjectitem)" style="width:100%;display:block" alt="专题图片">
    </div>
  </div>
</template>

<script>
  import { Card, Alert } from 'vux'
  import * as urls from '../../../api/urls'
  import {API_ROOT} from '../../../config.js'
  import $ from 'jquery'

  export default {
    components: {
      Card,
      Alert
    },
    mounted: function () {
      this.id = this.$route.query.id
      if (window.sessionStorage.getItem('userId') === '' || window.sessionStorage.getItem('userId') === null || window.sessionStorage.getItem('userId') === undefined) {
        let userId = window.location.href.replace(/.*userId=(\w*)&?.*/, '$1')
        let id = window.location.href.replace(/.*id=(\w*)&?.*/, '$1')
        this.userId = userId
        this.id = id
        if (this.userId !== '' && this.userId !== null && this.userId !== undefined) {
          window.sessionStorage.setItem('userId', this.userId)
          this.getList()
        } else {
          let url = window.location.origin
          window.location.href = url + '/api/wxAuth/index?type=t1'
        }
      } else {
        this.getList()
      }
    },
    data () {
      return {
        pic: ['https://cdn.xiaotaojiang.com/uploads/56/4b3601364b86fdfd234ef11d8712ad/_.jpg', 'https://cdn.xiaotaojiang.com/uploads/56/4b3601364b86fdfd234ef11d8712ad/_.jpg', 'https://cdn.xiaotaojiang.com/uploads/56/4b3601364b86fdfd234ef11d8712ad/_.jpg'],
        alertShow: {
          showmsg: false,
          msg: ''
        },
        subjectList: [],
        subject: {},
        id: ''
      }
    },
    methods: {
      getList () {
        let that = this
        this.http(urls.subjectDetail, {id: that.id}, function (res) {
          if (res.status === 100) {
            that.subject = res.data.subject
            if (res.data.items.length !== 0) {
              that.subjectList = res.data.items
            }
            that.diyweixinfn3()
          } else {
            that.alertShow.showmsg = true
            that.alertShow.msg = res.errMsg
          }
        }, function (res) {
          that.alertShow.showmsg = true
          that.alertShow.msg = res
        })
      },
      shopinfoFn (item) {
        if (item.itemOrNot === 1) {
          window.sessionStorage.setItem('shopId', item.itemId)
          let userId = window.sessionStorage.getItem('userId')
          window.sessionStorage.setItem('userId', userId)
          if (item.itemKind === 2) {
            window.location.href = '/api/wxAuth/go/ShopInfo_id=' + item.itemId + '&userId=' + userId
          } else if (item.itemKind === 3) {
            window.location.href = '/api/wxAuth/go/ScoreShop_id=' + item.itemId + '&userId=' + userId
          }
        }
      },
      onShow () {
        // 弹窗出现时触发
      },
      onHide () {
        // 弹窗关闭时触发
      },
      diyweixinfn3 () {
        let that = this
        let userId = window.sessionStorage.getItem('userId')
        let url = window.location.origin
        let link = url + '/api/wxAuth/index?type=t9&id=' + that.id + '&uid=' + userId
        that.$wechat.ready(function () {
          that.$wechat.onMenuShareAppMessage({
            title: that.subject.name, // 分享标题
            desc: that.subject.desc, // 分享描述
            link: link, // 分享链接
            imgUrl: that.subject.url[0], // 分享图标
            success: function () {
              // 用户确认分享后执行的回调函数
            },
            cancel: function () {
              // 用户取消分享后执行的回调函数
            }
          })
          that.$wechat.onMenuShareTimeline({
            title: that.subject.name, // 分享标题
            link: link, // 分享链接
            imgUrl: that.subject.url[0], // 分享图标
            success: function () {
              // 用户确认分享后执行的回调函数
            },
            cancel: function () {
              // 用户取消分享后执行的回调函数
            }
          })
        })
        $.ajax({
          url: API_ROOT + '/api/wxAuth/jsauth?url=' + encodeURIComponent(url + '/static/index.html'),
          type: 'post',
          success: function (t) {
            if (t.status === 100) {
              let sign = t.data.sign
              let weixinConfigdiyweixinfn3 = {
                debug: false,
                appId: sign.appId,
                timestamp: sign.timeStamp,
                nonceStr: sign.nonceStr,
                signature: sign.signature,
                jsApiList: ['onMenuShareAppMessage', 'chooseWXPay', 'onMenuShareTimeline']
              }
              // 授权
              that.$wechat.config(weixinConfigdiyweixinfn3)
              // 调用ready初始化
            } else {
              that.diyweixinfn3()
            }
          },
          error: function (t) {
            that.diyweixinfn3()
          }
        })
      }
    }
  }
</script>
<style scoped="scoped">
</style>
